def subsets(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    res = []
    dfs(nums, res, 0, [])
    return res


def dfs(nums, res, index, subset):
    '''

    :param nums: 要遍历的列表
    :param res: 返回结果
    :param index: 开始遍历的索引位置
    :param subset: 子集
    :return:
    '''
    res.append(subset.copy())
    # 结束条件
    if index == len(nums):
        return

    for i in range(index, len(nums)):
        subset.append(nums[i])
        dfs(nums, res, i + 1, subset)
        subset.pop()


nums = [1, 2, 3]
print(subsets(nums))
# [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
